package store.pay.icbc;

import java.io.ByteArrayInputStream;
import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import store.pay.PayResultObject;
import store.pay.PayResultService;
import store.util.XmlParser;
import cn.com.infosec.icbc.ReturnValue;

public class IcbcB2cServerPayResultService implements PayResultService {

	public boolean doFail(HttpServletRequest request,
			HttpServletResponse response, PayResultObject pro) {
		System.out.println("doFail-------------->" + new Date());
		return true;
	}

	public boolean doSuccess(HttpServletRequest request,
			HttpServletResponse response, PayResultObject pro) {
		System.out.println("doSuccess-------------->" + new Date());
		return true;
	}

	public boolean doVerifyFail(HttpServletRequest request,
			HttpServletResponse response, PayResultObject pro) {
		System.out.println("doVerifyFail-------------->" + new Date());
		return true;
	}

	public PayResultObject payResultObject(HttpServletRequest request) {
		IcbcB2cPayResultObject o = new IcbcB2cPayResultObject();
		try {
			//HttpUtils.bindBeanOnlyString(request, o, "GBK");
			//String merVAR=request.getParameter("merVAR");
			
			String notifyData = request.getParameter("notifyData");
			String signMsg = request.getParameter("signMsg");
			System.out.println(notifyData);
			System.out.println(signMsg);
			String notifyDataXml = new String(ReturnValue.base64dec(notifyData.getBytes()));
			System.out.println(notifyDataXml);
			SAXParserFactory spf = SAXParserFactory.newInstance();
			XmlParser xmlParser = new XmlParser();
			SAXParser saxParser = spf.newSAXParser();
			saxParser.parse(new ByteArrayInputStream(notifyDataXml.getBytes()), xmlParser);
			o = xmlParser.getIcbcB2cPayResultObject();
			o.setSignMsg(signMsg);
			o.setNotifyData(notifyDataXml);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return o;
	}

}
